package cn.iocoder.yudao.module.service.mq.consumer.prodcut;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.iocoder.yudao.framework.common.enums.AccountStatusEnum;
import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils;
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
import cn.iocoder.yudao.module.service.controller.admin.assistant.vo.ServiceAssistantSaveReqVO;
import cn.iocoder.yudao.module.service.dal.dataobject.assistant.ServiceAssistantDO;
import cn.iocoder.yudao.module.service.service.assistant.ServiceAssistantService;
import cn.iocoder.yudao.module.system.enums.product.ProductEnum;
import cn.iocoder.yudao.module.system.mq.message.product.ProductSendMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 针对 {@link ProductSendMessage} 的消费者
 *
 * @author PetePower
 */
@Component
@Slf4j
public class ServiceProductConsumer {

    @Resource
    private ServiceAssistantService serviceAssistantService;


//    public void onMessage(ProductSendMessage message) {
//        if (message.getCode().equals(ProductEnum.SERVICE_ACCOUNT.getCode())) {
//            log.info("[ServiceProductConsumer][消息内容({})]", message);
//            for (int i = 0; i < message.getNum(); i++) {
//                TenantUtils.execute(message.getTenantId(), () -> {
//                    @Valid ServiceAssistantSaveReqVO saveReqVO = new ServiceAssistantSaveReqVO();
//                    saveReqVO.setName("默认生成，请修改");
//                    saveReqVO.setExpireTime(message.getExpireTime());
//                    if (message.getExpireTime() != 0) {
//                        saveReqVO.setExpireAt(LocalDateTimeUtil.now().plusDays(message.getExpireTime()));
//                    }
//                    serviceAssistantService.createAssistant(saveReqVO);
//                });
//            }
//        }
//    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(ProductSendMessage message) {
        if (message.getCode().equals(ProductEnum.SERVICE_ACCOUNT.getCode())) {
            log.info("[ServiceProductConsumer][消息内容({})]", message);
            for (int i = 0; i < message.getNum(); i++) {
                TenantUtils.execute(message.getTenantId(), () -> {
                    // 更新
                    if (Objects.nonNull(message.getBizId())) {
                        ServiceAssistantDO data = serviceAssistantService.getAssistant(message.getBizId());
                        // 过期时间小于当前时间， 已过期
                        if (LocalDateTimeUtils.beforeNow(data.getExpireAt()) && Objects.equals(data.getStatus(), AccountStatusEnum.EXPIRE.getStatus())) {
                            data.setExpireTime(message.getExpireTime());
                            data.setExpireAt(LocalDateTimeUtil.now().plusDays(message.getExpireTime()));
                            if (Objects.isNull(data.getOwnerUserId())) {
                                data.setStatus(AccountStatusEnum.UN_USE.getStatus());
                            } else {
                                data.setStatus(AccountStatusEnum.USED.getStatus());
                            }
                        } else {
                            // 没过期， 追加
                            data.setExpireTime(data.getExpireTime() + message.getExpireTime());
                            data.setExpireAt(data.getExpireAt().plusDays(message.getExpireTime()));
                        }
                        serviceAssistantService.updateAssistant(data);
                    } else {
                        @Valid ServiceAssistantSaveReqVO saveReqVO = new ServiceAssistantSaveReqVO();
                        saveReqVO.setExpireTime(message.getExpireTime());
                        saveReqVO.setName("默认名称");
                        if (message.getExpireTime() != 0) {
                            saveReqVO.setExpireAt(LocalDateTimeUtil.now().plusDays(message.getExpireTime()));
                        }
                        serviceAssistantService.createAssistant(saveReqVO);
                    }
                });
            }
        }
    }

}
